<template lang="html">
  <div class="container" v-loading="loading">
    <div class="search">
        <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
              <el-input class="form_input" v-model="searchText" placeholder="请输入订单编号或客户姓名/设计师姓名/店铺名称/产品名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
              <el-button type="primary" :disabled="isShutdownAudit" size="small" @click="onAuidt">一键派厂</el-button>
            </el-form-item>
        </el-form>
    </div>

    <div class="tables">
      <vTable :tableData="tableData" ref="vTable" :tInData="tInData" @onCheckAll="onCheckAll" @onCheck="onCheck">
          <template slot="产品" slot-scope="scope">
            <div class="pro">
                <div class="pro_image">
                  <img :src="scope.row.picUrl" alt="" />
                </div>
                <div class="pro_info">
                  <div class="pro_name">{{scope.row.commodityName}}</div>
                  <div class="num_color">{{scope.row.materialName}}/{{scope.row.color}}/{{scope.row.laceName}}x{{scope.row.commodityNum}}</div>
                  <div class="address">地址：{{scope.row.address}}</div>
                </div>
            </div>
          </template>
          <template slot="客户" slot-scope="scope">
            <div class="userinfo">
              <p>ID：{{scope.row.memberId}}</p>
              <p>姓名：{{scope.row.userName}}</p>
              <p>手机号：{{scope.row.phone}}</p>
            </div>
          </template>
          <template slot="姓名签" slot-scope="scope">
            <div class="">{{scope.row.orderNameSign || scope.row.nameSign}}</div>
          </template>
          <template slot="备注" slot-scope="scope">
            <div class="remarks">
                <p>{{scope.row.remark}}</p>
                <el-button type="text" icon="el-icon-edit" @click="handleClick(scope.row)"></el-button>
            </div>
          </template>
          <template slot="状态" slot-scope="scope">
            <div class="">
              <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :inactive-value="2" :active-value="1" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
            </div>
          </template>
          <template slot="操作" slot-scope="scope">
              <el-button size="mini" type="primary" @click="handleEdit(scope.row)">派厂</el-button>
              <el-button size="mini" type="danger" @click="handleDetail(scope.row)">详情</el-button>
          </template>
      </vTable>

    </div>
<div class="total">成交合计：<span class="totalSpan">￥{{totalSSPrice}}</span> <span style="visibility:hidden">这里是分割线*************</span> 结算合计：<span class="totalSpan">￥{{totalSSPrice2}}</span></div>
    <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />


    <el-dialog title="派厂" v-loading="loading" :visible.sync="dialogVisible" :append-to-body="true" width="35%">
          <div class="audit_dialog">
              <el-radio v-for="(item,index) in factory" :key="index" v-model="radio" :label="item.id">{{item.factoryName}}</el-radio>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirm">确 定</el-button>
          </span>
    </el-dialog>
    <el-dialog title="查看备注" v-loading="loading" :visible.sync="dialogVisible2" :append-to-body="true" width="35%">
        <div class="">
          <el-input type="textarea" :rows="2" v-model="rows.remark"></el-input>
        </div>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible2 = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Deliver",
  data(){
    return {
      loading: false,
      searchText: '',
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
      dialogVisible: false,
      dialogVisible2: false,
      currentRowData: null,
      selectData:[],
      tableData: [],
      radio:null,
      factory:[],
      tInData:[
        {
          props: 'orderSublistId',
        },
        {
          props: 'createTime',
        },
        {
          title: '产品',
          width: '350px',
        },
        {
          title: '姓名签',
          width: '120px',
        },
        {
          title: '销售金额',
          props: 'commodityPrice',
          width: '120px',
        },
        {
          title: '店铺',
          props: 'branchStoreName',
          width: '200px',
        },
        {
          title: '客户',
          width: '200px',
        },
        {
          title: '设计师',
          props: 'designerName',
          width: '120px',
        },
        {
          title: '成交价格',
          props: 'successPrice',
          width: '120px',
        },
        {
          title: '结算价格',
          props: 'realPrice',
          width: '120px',
        },
        // {
        //   title: '余额',
        //   props: 'prepaidBalance',
        //   width: '80px',
        // },
        {
          title: '备注',
          width: '200px',
        },
        {
          title: '状态',
          width: '120px',
        },
        {
          title: '操作',
        },
      ],
      totalSSPrice:0,
      totalSSPrice2:0,
      rows: {}
    }
  },
  computed:{
    isShutdownAudit:function(){
        return this.selectData.length == 0?true:false;
    }
  },
  created(){
    this.selectOrderFactoryPage();
  },
  methods:{
    selectOrderFactoryPage(){
      this.loading = true;
      this.$http.selectOrderFactoryPage({
        query: this.searchText,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      }).then(res=>{
        this.loading = false;
        this.tableData = Object.assign([], res.data.rows);
        this.totalSSPrice = 0;
        this.totalSSPrice2 = 0;
        this.tableData.forEach(item=>{
          if(item.createTime){
            item.createTime = this.formatTime(item.createTime);
          }
          this.totalSSPrice = this.$parent.accAdd(this.totalSSPrice,item.successPrice);
          this.totalSSPrice2 = this.$parent.accAdd(this.totalSSPrice2,item.realPrice);
        });
        this.totalPage = res.data.total;
        this.pageSize = res.data.pageSize;
      });
    },
    formatTime(value) {
      if(!value) return '';
      let oDate = new Date(value);
      let h = oDate.getHours()<10?'0'+oDate.getHours():oDate.getHours();
      let m = oDate.getMinutes()<10?'0'+oDate.getMinutes():oDate.getMinutes();
      let s = oDate.getSeconds()<10?'0'+oDate.getSeconds():oDate.getSeconds();
      return oDate.getFullYear()+'-'+(oDate.getMonth()+1)+'-'+oDate.getDate()+' '+h+':'+m+':'+s;
    },
    selectFactoryList(){
      this.loading = true;
      this.$http.selectFactoryList().then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.factory = res.data.data;
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    factoryOrder(data){
      this.$http.factoryOrder(data).then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.dialogVisible = false;
          this.onSubmit();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    factoryOrders(data){
      this.$http.factoryOrders(data).then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.dialogVisible = false;
          this.onSubmit();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    handleClick(e){
      this.rows = e;
      this.dialogVisible2 = true;
    },
    save(){
      this.$http.updateOrderSublistRemark({
        id: this.rows.id,
        remark: this.rows.remark
      }).then(res=>{
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.onSubmit();
          this.dialogVisible2 = false;
        }else{
          this.$message.error(res.data.message);
        }
      })
    },
    onAuidt(){
      this.currentRowData = null;
      this.radio = null;
      this.dialogVisible = true;
      this.selectFactoryList();
    },
    handleEdit(e){
      this.currentRowData = e;
      this.radio = null;
      this.dialogVisible = true;
      this.selectFactoryList();
    },
    confirm(){
      if(!this.radio){
        this.$message.error('请选择工厂！');
        return false;
      }
      this.loading = true;
      if(this.currentRowData){
        let data = {
          orderSublistId: this.currentRowData.orderSublistId,
          factoryId: this.radio
        };
        this.factoryOrder(data);
      }else{
        let aId = [];
        this.selectData.forEach(item=>{aId.push(item.orderSublistId)});
        let data = {
          orderSublistIds: aId.join(','),
          factoryId: this.radio
        };
        this.factoryOrders(data);
      }

    },
    onCheckAll(val){
      if(val.status){
        this.selectData = [];
        this.tableData.forEach((item,index)=>{
          this.selectData.push(item);
        })
      }else{
        this.selectData = [];
      }
    },
    onCheck(val){
      if(val.status){
        this.tableData.forEach((item,index)=>{
          if(val.index == index){
            this.selectData.push(item);
          }
        })
      }else{
        this.selectData = this.selectData.filter(arr=>(arr.id!==this.tableData[val.index].id))
      }
    },
    onSwitch(e){
      this.$http.closeOrderFactory({orderSublistId: e.orderSublistId,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.$refs.vTable.onCheckAll(false);
      this.$refs.vTable.checkAll = false;
      this.selectData = [];
      this.currentPage = e;
      this.selectOrderFactoryPage();
    },

    handleDetail(e){
      let {href} = this.$router.resolve({ path: '/orders/deliver/order_detail', query: {id: e.orderSublistId}});
      window.open(href, '_blank');
    }

  }
}
</script>

<style lang="css" scoped>
</style>
